package com.xm.util;

import com.xm.biz.GoodsBiz;
import com.xm.biz.impl.GoodsBizImpl;
import com.xm.po.Goods;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : litieelo
 * @version : 1.0
 * @project : orange
 * @description : 获取天猫商品信息
 * @date : 2020-06-12 10:37
 */
public class GetProductInfo {

    private static GoodsBiz goodsBiz = new GoodsBizImpl();

    public static void main(String[] args) throws IOException {
        List<Goods> goodsList = goodsList("苹果");
        goodsList.forEach(goods -> {
            goodsBiz.add(goods);
        });
    }

    /**
     * 获取商品列表
     * @param goodsType
     * @return
     * @throws IOException
     */
    public static List<Goods> goodsList(String goodsType)throws IOException{
        //商品列表
        List<Goods>goodsList = new ArrayList<>();
        // 需要爬取商品信息的网站地址
        String url = "https://list.tmall.com/search_product.htm?q=" + goodsType;
        // 动态模拟请求数据
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        // 模拟浏览器浏览（user-agent的值可以通过浏览器浏览，查看发出请求的头文件获取）
        //httpGet.setHeader("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36");
        CloseableHttpResponse response = httpclient.execute(httpGet);
        // 获取响应状态码
        int statusCode = response.getStatusLine().getStatusCode();
        try {
            HttpEntity entity = response.getEntity();
            // 如果状态响应码为200，则获取html实体内容或者json文件
            if (statusCode == 200) {
                String html = EntityUtils.toString(entity, Consts.UTF_8);
                // 提取HTML得到商品信息结果
                Document doc = null;
                // doc获取整个页面的所有数据
                doc = Jsoup.parse(html);
                //输出doc可以看到所获取到的页面源代码
                //System.out.println(doc);
                // 通过浏览器查看商品页面的源代码，找到信息所在的div标签，再对其进行一步一步地解析
                Elements ulList = doc.select("div[class='view grid-nosku']");
                Elements liList = ulList.select("div[class='product']");
                // 循环liList的数据（具体获取的数据值还得看doc的页面源代码来获取，可能稍有变动）
                for (Element item : liList) {
                    //商品信息
                    Goods goods = new Goods();
                    // 商品ID
                    String id = item.select("div[class='product']").select("p[class='productStatus']").select("span[class='ww-light ww-small m_wangwang J_WangWang']").attr("data-item");
                    goods.setGoodsSn(id);
                    // 商品名称
                    String name = item.select("p[class='productTitle']").select("a").attr("title");
                    goods.setGoodsName(name);
                    goods.setGoodsDesc(name);

                    // 商品价格
                    String price = item.select("p[class='productPrice']").select("em").attr("title");
                    goods.setMarketPrice(Double.valueOf(price));
                    goods.setShopPrice(Double.valueOf(price));
                    // 商品网址
                    String goodsUrl = item.select("p[class='productTitle']").select("a").attr("href");
                    //System.out.println("商品网址：" + goodsUrl);
                    // 商品图片网址
                    String imgUrl = item.select("div[class='productImg-wrap']").select("a").select("img").attr("data-ks-lazyload");
                    goods.setGoodsImg(imgUrl);
                    goods.setGoodsThums(imgUrl);

                    //将商品加入集合
                    goodsList.add(goods);
                }
                // 消耗掉实体
                EntityUtils.consume(response.getEntity());
            } else {
                // 消耗掉实体
                EntityUtils.consume(response.getEntity());
            }
        } finally {
            response.close();
        }

        return goodsList;
    }
}